import getApiData from '@/services/cpasRequest';
import { Modal } from 'antd';
import notify from 'devextreme/ui/notify';

export async function checkMainType(dbname: string, mainType?: string): Promise<boolean> {
  // start ---  先检查, 是否存在mainType , 没有则给提示
  const apiSearchMainTypeParams: { name: string; params: any } = {
    name: 'get.BAS_Transaction_Type',
    params: {
      dbname: dbname,
    },
  };

  let errorContent = '';

  if (mainType) {
    apiSearchMainTypeParams.params.mainType = mainType;
    errorContent = `未检测到【${mainType}】标准业务类型, 请尝试手动设置.`;
  } else {
    errorContent = `未检测到标准业务类型, 请尝试手动设置标准业务类型`;
  }

  try {
    const mainTypeResponse = await getApiData(
      apiSearchMainTypeParams.name,
      apiSearchMainTypeParams.params,
    );
    if (mainTypeResponse.ok) {
      if (mainTypeResponse.data.length < 1) {
        Modal.warning({
          title: '警告',
          content: errorContent,
        });
        return false;
      } else {
        return true;
      }
    } else {
      notify(`获取业务类型数据接口异常!`, 'error', 2000);
      return false;
    }
  } catch (e) {
    notify(`获取业务类型数据接口异常!`, 'error', 2000);
    return false;
  }
  // end  先检查, 是否存在mainType , 没有则给提示
}

export function customSortZeroData(field: any, data: any, sortOrder?: any) {
  data.sort((a: any, b: any) => {
    const aValue = parseInt(a[field]);
    const bValue = parseInt(b[field]);
    if (aValue === 0) {
      return 1; // 将值为 0 的数据放在后面
    } else if (bValue === 0) {
      return -1; // 将值为 0 的数据放在后面
    } else {
      return bValue - bValue; // 默认排序规则
    }
  });

  if (sortOrder === 'desc') {
    data.reverse(); // 如果是降序排序，反转数组
  }
  return data;
}

export function customSortData(value1: any, value2: any) {
  // @ts-ignore
  // eslint-disable-next-line @typescript-eslint/no-this-alias
  const currentColumnConfig: any = this;
  const order = currentColumnConfig.sortOrder;
  if (value1 === 0) {
    if (order === 'desc') {
      return -1; // 将值为 0 的数据放在后面
    } else {
      return 1; // 将值为 0 的数据放在后面
    }
  } else if (value2 === 0) {
    if (order === 'desc') {
      return 1; // 将值为 0 的数据放在后面
    } else {
      return -1; // 将值为 0 的数据放在后面
    }
  } else {
    return value1 - value2; // 默认排序规则
  }
}

export async function getAccountInfo(params: { dbname: string }) {
  if (!params.dbname) return [];
  const res = await getApiData('account.accountInfo.getDataList', params);
  if (res.ok) {
    return Promise.resolve({
      data: res.data,
      success: true,
    });
  }
  return Promise.resolve({
    data: [],
    success: false,
  });
}
